package demo.test;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RequestMapping("/response")
@Controller
public class Demo3 {

    //响应

//    @RequestMapping("/index")
//    public String index(){
//        return "/Response.html";
//    }

    @ResponseBody
    @RequestMapping("/index")
    public String index(){
        return "/Response.html";
    }

    @RequestMapping("/data")
    public String data(){
        return "该方法返回的是一段数据";
    }


    //返回 HTML 片段

    @ResponseBody
    @RequestMapping("/resData")
    public String resData(){
        return "<h1>HTML片段</h1>";
    }


    //返回 JSON

    @ResponseBody
    @RequestMapping(value = "/setContentType", produces = "application/json")
    public String setContentType(){
        return "{\"OK\":1}";
    }

    @ResponseBody
    @RequestMapping("/getMap")
    public HashMap<String, String> getMap(){
        HashMap<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        map.put("key4", "value4");
        return map;
    }


    //设置状态码
    @ResponseBody
    @RequestMapping("/setStatus")
    public String serStatus(HttpServletResponse response){
        response.setStatus(418);
//        return "设置状态码";
        return response.getStatus() + " ";
    }


    //设置 Content-Type
    @ResponseBody
//    @RequestMapping(value = "/returnJson", produces = "application/json")
    @RequestMapping(value = "/returnJson", produces = "text/html")
    public String returnJson(){
        return "{\"success\":true}";
    }

    //设置 Header
    @ResponseBody
    @RequestMapping(value = "/setHeader")
    public String setHeader(HttpServletResponse response){
        response.setHeader("MyHeader","MyHeaderValue");
        return "设置 Header 成功";
    }
}
